package com.zengqingfa.juc.n3;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @fileName: TestInterrupt
 * @author: zengqf3
 * @date: 2021-6-8 16:11
 * @description:
 */
@Slf4j(topic = "c.TestInterrupt")
public class TestInterrupt {

    public static void main(String[] args) throws Exception {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                log.debug("park...");
                LockSupport.park();
                log.debug("打断状态：{}", Thread.currentThread().isInterrupted());
            }
        });
        t1.start();
        Thread.sleep(100);
        t1.interrupt();
    }

    private static void method4() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("park...");
            LockSupport.park();
            log.debug("unpark...");
            log.debug("打断状态：{}", Thread.currentThread().isInterrupted());
        }, "t1");
        t1.start();
        Thread.sleep(100);
        t1.interrupt();
        log.debug("打断状态：{}", t1.isInterrupted());
        //调用方法Thread.interrupted()清除打断标记
        Thread.interrupted();
        log.debug("调用方法Thread.interrupted()后的状态：{}", t1.isInterrupted());
    }

    private static void method3() throws InterruptedException {
        /**
         * 18:50:34:094 [t2] c.TestInterrupt - 当前线程是否被打断：true
         * 18:50:34:094 [t2] c.TestInterrupt -  打断状态: true
         * 18:50:34:094 [t2] c.TestInterrupt - 当前线程是否被打断：false
         */
        Thread t2 = new Thread(() -> {
            while (true) {
                Thread current = Thread.currentThread();
                boolean interrupted = current.isInterrupted();
                log.debug("当前线程是否被打断：{}", Thread.interrupted());
                if (interrupted) {
                    log.debug(" 打断状态: {}", interrupted);
                    //第二次调用清空了打断状态
                    log.debug("当前线程是否被打断：{}", Thread.interrupted());
                    break;
                }
            }
        }, "t2");
        t2.start();
        Thread.sleep(100);
        t2.interrupt();
    }

    private static void method1() throws InterruptedException {
        /**
         * 17:06:43:021 [main] c.TestInterrupt -  打断状态: false
         * java.lang.InterruptedException: sleep interrupted
         * 	at java.lang.Thread.sleep(Native Method)
         * 	at java.lang.Thread.sleep(Thread.java:340)
         * 	at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
         * 	at com.zengqingfa.juc.n3.TestInterrupt.lambda$main$0(TestInterrupt.java:19)
         * 	at java.lang.Thread.run(Thread.java:748)
         * 17:06:43:029 [main] c.TestInterrupt -  打断状态: false
         */
        Thread t1 = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");
        t1.start();
        TimeUnit.SECONDS.sleep(1);
        log.debug(" 打断状态: {}", t1.isInterrupted());
        t1.interrupt();
        log.debug(" 打断状态: {}", t1.isInterrupted());
    }
}
